<?php
/*
	
*/
defined('BY_JYA') or exit('Access Denied');
global $_RQ,$_AR;
$_RQ['op']=$_RQ['op']?$_RQ['op']:'get';
$return=array();
if($_RQ['op']=='get'){
	$id=intval($_RQ['id']);
	$mid=intval($_RQ['mid'])?:$_SESSION['uid'];
	if(!$id){
		exi('没有找到该活动，请重新选择！','error','jy_h5ewm/index/');
	}
	pdo_update('jy_h5ewm_list',array('num +='=>1),array('uniacid'=>$_SESSION['uniacid'],'id'=>$id));
	$item=pdo_get('jy_h5ewm_list',array('uniacid'=>$_SESSION['uniacid'],'id'=>$id));
	if($item['link'] && !$_RQ['issao'] && !$_RQ['mid']){
		exi('','error',$item['link']);
	}
	$item['images']=json_decode($item['images'],true);
	$item['data']=json_decode($item['data'],true);
	$item['diyform']=json_decode($item['diyform'],true);
	if($item['isauth']){
		$_SESSION['forward_url']='jy_h5ewm/item//id='.$item['id'];
		$return['ischecklogin']=cfc('core')->checklogin('','',true);
		$return['show_setmobile']=cfc('core')->check_setmobile(1);
		$return['show_setmember']=cfc('core')->check_setmember(1);
		if($item['link'] && $_RQ['mid'] && $_RQ['mid']!=$_SESSION['uid']){
			exi('','error',$item['link']);
		}
		if($item['link'] && $_RQ['issao'] && $_SESSION['uid']){
			exi('','error','jy_h5ewm/item//id='.$item['id'].'&mid='.$_SESSION['uid']);
		}
	}
	$isposter=pdo_get('jy_h5ewm_poster',array('mid'=>$_SESSION['uid'],'uniacid'=>$_SESSION['uniacid'],'listid'=>$item['id']));
	$isposter['diyform']=json_decode($isposter['diyform'],true);
	if($mid && $item['isdiyform']){
		$return['poster']=$isposter['poster'];
	}
	if($mid && !$return['poster']){
		$get_fansinfo=cfc('fans')->get_fansinfo($mid);
		$openid=$get_fansinfo['openid']?:$_SESSION['openid'];
		$member=model('core/member')->detail($mid,array('credit1','credit2','mobile','avatar','nickname'));
		$url=custom_url($item['link'],true);
		$qr=cfc('tools')->createqrcode($url,'250',true);
		$qr=tomedia($qr);
		$image='oss/'.$_SESSION['uniacid'].'/images/jy_h5ewm/wxappid'.$mid.'.jpg';
		$isadd=cfc('files')->source_check($image);
		if(!$isadd && $_SESSION['wxapp_id']){
				if($_SESSION['fromh5wxapp']){
					$url=$item['link'];
					$wxappurl='/pages/index/index?url='.urlencode($url);
				}else{
					$wxappurl=$item['link'];
				}
				$wxapp=pdo_get('core_account_wxapp',array('id'=>$_SESSION['wxapp_id'],'uniacid'=>$_SESSION['uniacid']));
				$res=model('core/wxapp')->appcode($wxappurl,$wxapp['appid'],$wxapp['appsecret'],200);
				$path='oss/'.$_SESSION['uniacid'].'/images/jy_h5ewm/';
				if(!file_exists(ROOT_AT.$path)){
					mkdir(ROOT_AT.$path,0700, true);
				}
				$filename=$path.'/wxappid'.$mid.'.jpg';
				if($res){
					$qr=cfc('files')->source_save($res,'image','png','二维码',$filename,'',$wxappurl);
				}
		}
	}
	$nickname=$member['nickname']?:$member['username'];
	$riqi=date('Y年m月d日');
	$item['title2']=str_replace('[昵称]',$nickname,$item['title2']);
	$item['title2']=str_replace('[日期]',$riqi,$item['title2']);
	$item['desc2']=str_replace('[昵称]',$nickname,$item['desc2']);
	$item['desc2']=str_replace('[日期]',$riqi,$item['desc2']);
	foreach($item['data']['data'] as &$dv){
		if($dv['type']=='title'){
			$dv['type']='text';
			$dv['value']=$item['title2'];
		}elseif($dv['type']=='desc'){
			$dv['type']='text';
			$dv['value']=$item['desc2'];
		}
	}
	if((!$item['isauth'] || $mid) && !$return['poster'] && !$item['isdiyform']){
		$return=plugin('jy_poster')->get_active('','','',$openid,$qr,$item['data'],'jy_h5ewm','',true);
		$return['poster']=$return['poster'];
		$return['listid']=$item['id'];
		$this->model('model')->upposter($return);
	}
	$setting=$this->plugin_setting('basic');
	$return['setting']=$setting;
	$return['item']=$item;
	$return['post']=array(
		'listid'=>$item['id'],
		'diyform'=>$isposter['id']?$isposter['diyform']:$item['diyform'],
	);
	if($item['isdiyform'] && (!$item['isauth'] || $mid) && !$return['poster'] && $mid==$_SESSION['uid']){
		$return['isdiyform']=1;
	}
	$link='jy_h5ewm/item//id='.$item['id'].'&fromuser='.$_SESSION['uid'];
	$return['sharedata']=array(
		'title'=>$item['title'],
		'desc'=>$item['desc'],
		'link'=>custom_url($link,true),
		'imgUrl'=>tomedia($item['image']),
	);
	set_html('title',$item['title']);
	exi($return);
}
if($_RQ['op']=='upposter'){
	$post=get_postdata();
	$id=$post['listid'];
	$mid=intval($_RQ['mid'])?:$_SESSION['uid'];
	if(!$id){
		exi('没有找到该活动，请重新选择！','error','jy_h5ewm/index/');
	}
	$item=pdo_get('jy_h5ewm_list',array('uniacid'=>$_SESSION['uniacid'],'id'=>$id));
	$item['images']=json_decode($item['images'],true);
	$item['data']=json_decode($item['data'],true);
	if($mid){
		$get_fansinfo=cfc('fans')->get_fansinfo($mid);
		$openid=$get_fansinfo['openid']?:$_SESSION['openid'];
		$member=model('core/member')->detail($mid,array('credit1','credit2','mobile','avatar','nickname'));
		$url=custom_url($item['link'],true);
		$qr=cfc('tools')->createqrcode($url,'250',true);
		$qr=tomedia($qr);
		$image='oss/'.$_SESSION['uniacid'].'/images/jy_h5ewm/wxappid'.$mid.'.jpg';
		$isadd=cfc('files')->source_check($image);
		if(!$isadd && $_SESSION['wxapp_id']){
				if($_SESSION['fromh5wxapp']){
					$url=$item['link'];
					$wxappurl='/pages/index/index?url='.urlencode($url);
				}else{
					$wxappurl=$item['link'];
				}
				$wxapp=pdo_get('core_account_wxapp',array('id'=>$_SESSION['wxapp_id'],'uniacid'=>$_SESSION['uniacid']));
				$res=model('core/wxapp')->appcode($wxappurl,$wxapp['appid'],$wxapp['appsecret'],200);
				$path='oss/'.$_SESSION['uniacid'].'/images/jy_h5ewm/';
				if(!file_exists(ROOT_AT.$path)){
					mkdir(ROOT_AT.$path,0700, true);
				}
				$filename=$path.'/wxappid'.$mid.'.jpg';
				if($res){
					$qr=cfc('files')->source_save($res,'image','png','二维码',$filename,'',$wxappurl);
				}
		}
	}
	$nickname=$member['nickname']?:$member['username'];
	$nickname=$post['nickname']?:$nickname;
	$riqi=date('Y年m月d日');
	$diyform=$post['diyform'];
	$search=array(
		'昵称'=>$nickname,
		'日期'=>$riqi,
	);
	foreach($diyform as $fv){
		$search[$fv['title']]=$fv['value'];
	}
	foreach($item['data']['data'] as &$dv){
		if($dv['type']=='title'){
			$dv['type']='text';
			$dv['value']=$item['title2'];
			$dv['value']=cfc('tools')->replace($search,$dv['value']);
		}elseif($dv['type']=='desc'){
			$dv['type']='text';
			$dv['value']=$item['desc2'];
			$dv['value']=cfc('tools')->replace($search,$dv['value']);
		}elseif($dv['type']=='text'){
			$dv['value']=cfc('tools')->replace($search,$dv['value']);
		}
	}
	$item['data']['avatar']=$post['avatar'];
	$item['data']['nickname']=$post['nickname'];
	$poster=plugin('jy_poster')->get_active('','','',$openid,$qr,$item['data'],'jy_h5ewm','',true);
	$poster['posterimg']=$poster['poster'];
	$post['poster']=$poster['poster'];
	$this->model('model')->upposter($post);
	exi($poster);
}
